// component/planing/index.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    role: {
      type: String,
      value: "",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchValue: "",
    list: [],
    isLastPage: false,
    current: 1,
    size: 10,
    show:false,
    delId:null,
  },

  lifetimes: {
    attached: function () {
      this.getData();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
	pageLifetimes: {
    show: function () {
      this.onPullDownRefresh();
    }
  },

  methods: {
    //市场采购单完成   orderComplete
    finished(event){
      console.log(event,'eee')
      const id = event.currentTarget.dataset.id;
      this.setData({
        // show:true,
        delId:id
      })
			Dialog.confirm({
				context:this,
				title: '提示',
				message: '确定完成备货吗？',
			})
				.then(() => {
					this.delete(this.data.delId);
				})
				.catch(() => {
				});
    },
		// onSure(event) {
    //   this.delete(this.data.delId);
    // },
		// onClose() {
    //   this.setData({ show: false });
    // },

		delete(id) {
      wx.showLoading({
        title: '完成中...',
      })
      wx.$apis.orderComplete({
        ids: [id],
      }).then(res => {
        this.getData();
        if (!res.data) {
          return wx.showToast({
            title: '操作失败，请稍后再试~',
            icon: 'none',
            duration: 2000
          })
        }
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 2000
        })
      })
    },
    //小工完成备货
		pickgoods(e) {
			const id = e.currentTarget.dataset.id
			wx.navigateTo({
				url: "/pages/dashboard/hzdetail/hzdetail?id=" + id+"&type=1"
			})
		},

    //修改采购单
    changInfo(e) {
      console.log(e)
      if (this.data.role == 3) {
        return
      }
      let id= e.currentTarget.dataset.id
      wx.navigateTo({
        url: "/pages/order/addplan/addplan?id=" + id+"&type=1"

      })

    },
    //下拉刷新
    onPullDownRefresh() {
      this.setData({
        current: 1,
        list: [],
        isLastPage: false
      })
      this.getData()
    },
    //上滑加载
    onReachBottom() {
      let {
        current,
        isLastPage
      } = this.data
      if (isLastPage) {
        return
      }
      this.setData({
        current: (Number(current) + 1)
      })
      this.getData()
    },
		//搜索
		onSearch(e) {
			this.setData({
				current: 1,
				list: [],
				isLastPage: false,
				searchValue: e.detail
			})

			console.log('搜索', this.data.searchValue)
			this.getData();
		},
    getData() {
      //进行中   市场调用采购单分页  小工调用备货单分页
      const {
        role,
        current,
        size
      } = this.data;
      let api, parmas;
      if (role == 1) {//市场调用采购单分页
        api = "orderMarketerPage";
        parmas = {
          companyId: app.globalData.userInfo.companyData.companyId,
          current,
          size,
					goodsOwnerName:this.data.searchValue
        }
      } else {//小工调用备货单分页
        api = "marketerPage";
        parmas = {
          outStockStatus: 2,
          companyId: app.globalData.userInfo.companyData.companyId,
          current,
          size,
					goodsOwnerName:this.data.searchValue
        }
      }
      wx.showLoading({
        title: "正在请求数据中"
      })
      wx.$apis[api](parmas).then(res => {
        wx.stopPullDownRefresh();
				if(res.data){
					let old = this.data.list;
					let allList = res.data && res.data.records.map(item => {
						if(item.createTime){
							item.createTime=item.createTime.replace("T", " ")
						}
						return item
					})
					if (allList && allList.length) {
						let newList = old.concat(allList)
						this.setData({
							list: newList
						})
						if (allList.length < size) {
							this.setData({
								isLastPage: true
							})
						}
					} else {
						this.setData({
							isLastPage: true
						})
					}
				}

      }).catch(err => {
        wx.hideLoading()
      })
    },
  }
})
